package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.executor.ThreadPool;
import com.atguigu.feign.ProductFeignClient;
import com.atguigu.feign.SearchFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author ：Hachimen
 * @date ：Created in 2021/8/26 11:20
 * @description：Web控制器
 * @modified By：
 * @version: 0.0.1
 */
@Controller
public class WebSkuDetailController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private SearchFeignClient searchFeignClient;

    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> map = new HashMap<>();
        //查询SKU基础信息
        CompletableFuture<SkuInfo> completableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        },ThreadPool.getThreadPoolExecutor());

        //进行热度记录
        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
            //TODO 不启动搜索微服务 暂时注掉
            // searchFeignClient.incrHotScore(skuId);
        },ThreadPool.getThreadPoolExecutor());

        //查询SKU所在的SPU信息(商品属性)
        CompletableFuture<Void> skuSalePropertyKeyAndValueFuture = completableFuture.thenAcceptAsync(skuInfo -> {
            List<ProductSalePropertyKey> skuSalePropertyKeyAndValue = productFeignClient.getSkuSalePropertyKeyAndValue(skuInfo.getProductId(), skuId);
            map.put("skuSalePropertyKeyAndValueList", skuSalePropertyKeyAndValue);
        },ThreadPool.getThreadPoolExecutor());

        //根据skuId 查询商品的价格
        CompletableFuture<Void> skuPriceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price",skuPrice);
        },ThreadPool.getThreadPoolExecutor());

        //获取sku的分类信息
        CompletableFuture<Void> categoryViewFuture = completableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        },ThreadPool.getThreadPoolExecutor());

        //通过spuId 获取对应的Json 字符串
        CompletableFuture<Void> skuSalePropertyValueIdFuture = completableFuture.thenAcceptAsync(skuInfo -> {
            Map skuSalePropertyValueId = productFeignClient.getSkuSalePropertyValueId(skuInfo.getProductId());
            map.put("salePropertyValueIdJson", JSON.toJSONString(skuSalePropertyValueId));
        },ThreadPool.getThreadPoolExecutor());

        CompletableFuture.allOf(completableFuture,
                hotScoreFuture,
                skuSalePropertyKeyAndValueFuture,
                skuPriceFuture,
                categoryViewFuture,
                skuSalePropertyValueIdFuture).join();
        model.addAllAttributes(map);
        return "detail/index";
    }

}
